package com.titan.core.utils.object;

import com.titan.core.utils.dbinfo.bean.ColumnBean;

/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月14日
 */
public class TableColumnDescriptor {
    private String tablename;  // 对应表英文名 nameZh
    private String nameEnglish;
    private String nameChina;
    private String comments;
    private int dataLength;
    private int dataPrecision;
    private int dataScale;
    private String sqlType;
    private Class javaType;
    private boolean nullable;   // 是否可以为空  true:可以为空   false:不能为空
    
    public TableColumnDescriptor() {
    }


    public String toString() {
        StringBuffer info =new StringBuffer();
        info.append("name_english="+nameEnglish+",");
        info.append("name_china="+nameChina+",");
        info.append("sqlType="+sqlType+",");
        info.append("javaType="+(this.getJavaType()==null?"null":this.getJavaType().getName())+",");
        info.append("datalength="+dataLength+",");
        info.append("dataprecision="+dataPrecision+",");
        info.append("datascale="+dataScale+",");
        info.append("comments="+comments+",");
        info.append("tablename="+tablename+",");
        info.append("nullable="+nullable);
        return info.toString();
    }

    
    public String getTablename() {
        return this.tablename;
    }

    public void setTablename(String tablename) {
        this.tablename = tablename;
    }

    public String getNameEnglish() {
        return this.nameEnglish;
    }
    
    public void setNameEnglish(String nameEnglish) {
        this.nameEnglish = nameEnglish;
    }
    
    public String getNameChina() {
        return this.nameChina;
    }
    
    public void setNameChina(String nameChina) {
        this.nameChina = nameChina;
    }
    
    public String getComments() {
        return this.comments;
    }
    
    public void setComments(String comments) {
        this.comments = comments;
    }
    
    public int getDataLength() {
        return this.dataLength;
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }
    
    public int getDataPrecision() {
        return this.dataPrecision;
    }
    
    public void setDataPrecision(int dataPrecision) {
        this.dataPrecision = dataPrecision;
    }

    public int getDataScale() {
        return this.dataScale;
    }

    public void setDataScale(int dataScale) {
        this.dataScale = dataScale;
    }

    public String getSqlType() {
        return this.sqlType;
    }
    
    public void setSqlType(String sqlType) {
        this.sqlType = sqlType;
    }

//    public Class getJavaType() {
//        return this.javaType;
//    }
    public Class getJavaType() {
        if(this.javaType!=null) {
            return this.javaType;
        }
        
        String sqltype = this.getSqlType();
        int length = this.getDataPrecision();
        int datascale = this.getDataScale();
//        System.out.println(column.getNameEn()+ "  sqltype="+sqltype + "   datascale="+datascale);
        if("VARCHAR2".equalsIgnoreCase(sqltype)
                || "VARCHAR".equalsIgnoreCase(sqltype)
                || "CHAR".equalsIgnoreCase(sqltype)
                || "TinyText".equalsIgnoreCase(sqltype)
                || "Text".equalsIgnoreCase(sqltype)
                || "MediumText".equalsIgnoreCase(sqltype)
                || "LongText".equalsIgnoreCase(sqltype)
                || "nvarchar".equalsIgnoreCase(sqltype)
                || "nchar".equalsIgnoreCase(sqltype)
                || "bit".equalsIgnoreCase(sqltype)
                ) {
            this.javaType = String.class;
        }
        if("TinyBlob".equalsIgnoreCase(sqltype)
                || "Blob".equalsIgnoreCase(sqltype)
                || "MediumBlob".equalsIgnoreCase(sqltype)
                || "LongBlob".equalsIgnoreCase(sqltype) 
                ) {
            this.javaType = byte[].class;
        }
        if( "DATE".equalsIgnoreCase(sqltype) || "datetime".equalsIgnoreCase(sqltype) ) {
            this.javaType = java.util.Date.class;
        }
        if( "int".equalsIgnoreCase(sqltype) || "mediumint".equalsIgnoreCase(sqltype) || "smallint".equalsIgnoreCase(sqltype) || "tinyint".equalsIgnoreCase(sqltype) ) {
            this.javaType = Integer.class;
        }
        if( "bigint".equalsIgnoreCase(sqltype) ) {
            this.javaType = Long.class;
        }
        if( "float".equalsIgnoreCase(sqltype) ) {
            this.javaType = Float.class;
        }
        if( "double".equalsIgnoreCase(sqltype) ) {
            this.javaType = Double.class;
        }
        if( "decimal".equalsIgnoreCase(sqltype) || "NUMBER".equalsIgnoreCase(sqltype) || "money".equalsIgnoreCase(sqltype) ) {
            if(datascale != 0) {
                if( length<=9 ) {
                    this.javaType = Float.class;
                } else {
                    this.javaType = Double.class;
                }
            } else {
                if( length<=9 ) {
                    this.javaType = Integer.class;
                } else {
                    this.javaType = Long.class;
                }
            }
        }
        return this.javaType;
    }    

    public void setJavaType(Class javaType) {
        this.javaType = javaType;
    }

    public boolean getNullable() {
        return this.nullable;
    }
    
    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }
}
